package com.example.common.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Knife4j配置类 - 用于配置API文档
 * 提供Swagger接口文档的可视化界面，方便前后端开发和接口调试
 * http://localhost:8080/doc.html
 * @author crp
 * @since 2025-09-23
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    /**
     * 用户认证API分组
     */
    @Bean
    public Docket userAuthApi() {
        List<Parameter> pars = new ArrayList<>();
        
        // Authorization参数
        ParameterBuilder authPar = new ParameterBuilder();
        authPar.name("Authorization")
                .description("JWT访问令牌，格式：Bearer {token}")
                .defaultValue("Bearer ")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(authPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户认证")
                .apiInfo(userAuthApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scale.service.user.controller"))
                .paths(PathSelectors.regex("/api/auth/.*"))
                .build()
                .globalOperationParameters(pars);
    }

    /**
     * 系统管理API分组
     */
    @Bean
    public Docket systemApi() {
        List<Parameter> pars = new ArrayList<>();
        
        // Authorization参数
        ParameterBuilder authPar = new ParameterBuilder();
        authPar.name("Authorization")
                .description("JWT访问令牌，格式：Bearer {token}")
                .defaultValue("Bearer ")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(authPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("系统管理")
                .apiInfo(systemApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scale.service.system.controller"))
                .paths(PathSelectors.regex("/api/system/.*"))
                .build()
                .globalOperationParameters(pars);
    }

    /**
     * 全部API分组
     */
    @Bean
    public Docket allApi() {
        List<Parameter> pars = new ArrayList<>();
        
        // Authorization参数
        ParameterBuilder authPar = new ParameterBuilder();
        authPar.name("Authorization")
                .description("JWT访问令牌，格式：Bearer {token}")
                .defaultValue("Bearer ")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(authPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("全部接口")
                .apiInfo(allApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scale.service"))
                .paths(PathSelectors.regex("/api/.*"))
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo userAuthApiInfo() {
        return new ApiInfoBuilder()
                .title("Scale AI聊天系统 - 用户认证API")
                .description("用户注册、登录、密码重置等认证相关接口")
                .version("1.0.0")
                .contact(new Contact("crp", "https://github.com/scale-ai-chat", "crp@example.com"))
                .build();
    }

    private ApiInfo systemApiInfo() {
        return new ApiInfoBuilder()
                .title("Scale AI聊天系统 - 系统管理API")
                .description("系统配置、角色管理、反馈等系统管理接口")
                .version("1.0.0")
                .contact(new Contact("crp", "https://github.com/scale-ai-chat", "crp@example.com"))
                .build();
    }

    private ApiInfo allApiInfo() {
        return new ApiInfoBuilder()
                .title("Scale AI聊天系统 - 完整API文档")
                .description("AI角色扮演语音聊天系统的所有后端接口文档")
                .version("1.0.0")
                .contact(new Contact("crp", "https://github.com/scale-ai-chat", "crp@example.com"))
                .build();
    }
}
